//
//  CZWelcomeViewController.swift
//  CY--weiBo
//
//  Created by mac on 15/11/28.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

import SDWebImage
class CZWelcomeViewController: UIViewController {

    //MARK: - 属性
    /// iconView底部约束
    private var iconViewBottomCon: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        prepareUI()
        
        //显示用户的头像
        if let avatar_large = CZUserAccount.loadUserAccount()?.avatar_large
        {
            iconView.sd_setImageWithURL(NSURL(string: avatar_large), placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
        //获取用户数据
        CZUserAccount.loadUserAccount()?.loadUserInfo()
        
    }
    
    /// 在用户看得到View的时候开始做动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //做动画
        moveAnimation()
        
    }
    //MARK: - 头像动画
    private func moveAnimation() {
    
        iconViewBottomCon?.constant = -500
        
        //弹簧效果的动画
        UIView.animateWithDuration(2, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                print("动画完成")
               //切换界面
            AppDelegate.switchRootViewController(true)
        }
    }
    
    
    //MARK: 准备UI
    private func prepareUI() {
//        1.添加子控件
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        //2.添加约束
        bkgImageView.ff_Fill(view)
        
      let cons = iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        iconViewBottomCon = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        
        messageLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 16))
    
    }
    
    
    //MARK: - 懒加载控件
    //背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var iconView: UIImageView = {
    
        let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        //设置圆角 宽高85
        view.layer.cornerRadius = 42.5
        view.layer.masksToBounds = true
        return view
    }()
    /// 消息label
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.sizeToFit()
        return label
    }()
    

}
